//
//  GameCentreVC.m
//  PlayCatchGame
//
//  Created by Sugar Chestnut on 2018/3/5.
//  Copyright © 2018年 清正. All rights reserved.
//

#import "GameCentreVC.h"
#import "AppHeader.h"
#import "HomeViewController.h"      // 借用 导航又按钮设置颜色
#import "MyAssetInfoVC.h"           // 借用 充值弹框
#import "GameSetView.h"
#import "OtherAlertView.h"

// 每次左右移动的距离
#define left_right_space 2.0
// 每次上下移动的距离
#define top_bottom_space 1.0
// 每次宽度变化
#define width_space 1.5


@interface GameCentreVC () {
    // 钩子最小左间距
    CGFloat left_space_minX;
    // 钩子最大左间距
    CGFloat left_space_maxX;
    
    // 钩子最小上间距
    CGFloat top_space_minY;
    // 钩子最大上间距
    CGFloat top_space_maxY;
    
    // 钩子最小宽度
    CGFloat rock_width_min;
    // 钩子最大宽度
    CGFloat rock_width_max;

}

/// 抓手顶部距顶部距离 (默认80)
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *rock_top;
/// 抓手左边距左边距离 (默认5)
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *rock_left;
/// 抓手的宽度 (默认65)
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *rock_width;

@property (strong, nonatomic) UIButton *navRightButton;
@property (weak, nonatomic) IBOutlet UITableView *btmTableView;
/// 开始按钮
@property (weak, nonatomic) IBOutlet UIButton *gameStartBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *startBtn_centerX;
/// 方向背景视图(子视图标志:700-703)
@property (weak, nonatomic) IBOutlet UIView *directionView;

@property (strong, nonatomic) GameSetView *gameSetView;

@end

@implementation GameCentreVC

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.translucent =YES;
    self.navigationController.navigationBar.barTintColor =[UIColor clearColor];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.translucent =NO;
    self.navigationController.navigationBar.barTintColor =APP_DEFAULT_COLOUR;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.gameType =0;
    [self initOtherView];
    // 长按手势
    [self addLongPressGestureRecognizer];
    
    rock_width_max =65;
    rock_width_min =rock_width_max/2.0;
    if (IS_IPHONE_6) {
        left_space_minX =5;
        top_space_minY =82;
        top_space_maxY =105;
    } else if (IS_IPHONE_6PLUS) {
        left_space_minX =10;
        top_space_minY =92;
        top_space_maxY =118;
    } else if (IS_IPHONE_X) {
        left_space_minX =5;
        top_space_minY =82;
        top_space_maxY =107;
    } else {
        left_space_minX =0;
        top_space_minY =70;
        top_space_maxY =90;
    }
    left_space_maxX =KScreenWidth-10-left_space_minX*2.0-rock_width_max;
    
    self.rock_top.constant =top_space_minY;
    self.rock_left.constant =left_space_minX;
    self.rock_width.constant =rock_width_max;
}


- (void)initOtherView {
    [self.navRightButton addTarget:self action:@selector(navRightItemClick:) forControlEvents:UIControlEventTouchUpInside];
    
    self.gameStartBtn.adjustsImageWhenHighlighted =NO;
    self.gameStartBtn.adjustsImageWhenDisabled =NO;
    [self.gameSetView didButtonClickBlock:^(UIButton *button) {
        if (button == _gameSetView.explainBtn) {
            [self showExplainAlertView];
        } else if (button == _gameSetView.musicButton) {
            if (button.selected) {
                NSLog(@"播放");
            } else {
                NSLog(@"静音");
            }
        } else if (button == _gameSetView.numberBtn) {
            if (button.selected) {
                NSLog(@"展开");
            } else {
                NSLog(@"闭合");
            }
        }
    }];
    self.gameSetView.watchNumber =@"1";
    [self.view addSubview:self.gameSetView];
}
/// 方向键添加长按手势
- (void)addLongPressGestureRecognizer {
    for (UIButton *button in self.directionView.subviews) {
        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(buttonLongPressGesture:)];
        longPress.minimumPressDuration = 0.8; //定义按的时间
        [button addGestureRecognizer:longPress];
    }
}


- (GameSetView *)gameSetView {
    if (!_gameSetView) {
        _gameSetView =[GameSetView loadNib:@"GameSetView" frame:CGRectMake(KScreenWidth-190, 60+getNavHeight(self.navigationController), 180, 130)];
        _gameSetView.backgroundColor =[UIColor clearColor];
    }
    return _gameSetView;
}

- (UIButton *)navRightButton {
    if (!_navRightButton) {
        _navRightButton =[UIButton buttonWithType:UIButtonTypeCustom];
        _navRightButton.frame = CGRectMake(0, 0, KScreenWidth/5, 30);
        _navRightButton.backgroundColor =[UIColor whiteColor];
        _navRightButton.cornerRadius =14;
        [_navRightButton setAttributedTitle:[HomeViewController rightBtnAttributedString:@" 0 +"] forState:UIControlStateNormal];
        [_navRightButton setImage:[UIImage imageNamed:@"jindou-2"] forState:UIControlStateNormal];
        _navRightButton.titleLabel.font =[UIFont systemFontOfSize:16];
        [_navRightButton setTitleColor:[UIColor colorWithRed:0.41 green:0.41 blue:0.41 alpha:1.00] forState:UIControlStateNormal];
        UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:_navRightButton];
        self.navigationItem.rightBarButtonItem =item;
    }
    return _navRightButton;
}

- (void)setGameState:(NSInteger)gameState {
    _gameState =gameState;

    if (gameState ==0) {
        self.directionView.hidden =YES;
        self.startBtn_centerX.constant =0;
    } else {
        self.directionView.hidden =NO;
        self.startBtn_centerX.constant =KScreenWidth/4.0;
    }
}


/// 导航右边按钮
- (void)navRightItemClick:(UIButton*)button {
    [MyAssetInfoVC rechargeAlertView_rechargeRusult:^(NSInteger type) {
        NSLog(@"type=%ld", type);
    }];
}

- (IBAction)buttonClick:(UIButton *)button {
    if (button ==self.gameStartBtn) {
        if (button.selected) {
            
        } else {
            button.selected =! button.selected;
            self.gameState =button.selected;
        }
    } else if (button.tag >=700) {
        [self rockMovingRange:button];
    }
}

static dispatch_source_t source_t =nil;
- (void)buttonLongPressGesture:(UILongPressGestureRecognizer *)longPress {
    if ([longPress state] == UIGestureRecognizerStateBegan) {
        if (source_t) {
            dispatch_source_cancel(source_t);
            source_t =nil;
        }
        source_t =[TimerHandler openTimerEveryTime:0.05 eventHandler:^(dispatch_source_t timer) {
            if ([longPress.view isKindOfClass:[UIButton class]]) {
                UIButton *button =(UIButton*)longPress.view;
                [self rockMovingRange:button];
            }
        }];
    } else if ([longPress state] == UIGestureRecognizerStateEnded) {
        if (source_t) {
            dispatch_source_cancel(source_t);
            source_t =nil;
        }
    }
}

/// 显示游戏说明
- (void)showExplainAlertView {
    AlertBgView *alertView =[[AlertBgView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight) clickRemove:YES bgColor:[UIColor colorWithWhite:0 alpha:0.4]];
    [alertView showView];
    alertView.centerView_center.constant =-20;
    alertView.centerView_height.constant =300;
    
    CGFloat width =300;
    OtherAlertView *packetView =[OtherAlertView loadNib:@"OtherAlertView" frame:CGRectMake((KScreenWidth-width)/2.0, 0, width, 300)];
    [packetView didButtonClickBlock:^(UIButton *button) {
        [packetView removeFromSuperview];
        [alertView removeView];
    }];
    [alertView.centerView addSubview:packetView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/// 控制方向
- (void)rockMovingRange:(UIButton *)button {
    if (button.tag ==700) {
        if (self.rock_width.constant >=rock_width_max ||
            self.rock_top.constant <=top_space_minY) {
            return;
        }
        self.rock_top.constant -=top_bottom_space;
        self.rock_width.constant +=width_space;
        if (self.rock_left.constant <=(KScreenWidth-20-rock_width_max)/2.0) {
            self.rock_left.constant -=top_bottom_space+0.3;
        }
    } else if (button.tag ==701) {
        CGFloat ratio_space =(rock_width_max-self.rock_width.constant)-(rock_width_max/self.rock_width.constant -1) *5.0;
        if (self.rock_left.constant <=left_space_minX+ratio_space) {
            return;
        }
        self.rock_left.constant -=left_right_space;
    } else if (button.tag ==702) {
        if (self.rock_width.constant <=rock_width_min ||
            self.rock_top.constant >=top_space_maxY) {
            return;
        }
        self.rock_top.constant +=top_bottom_space;
        self.rock_width.constant -=width_space;
        if (self.rock_left.constant <=(KScreenWidth-20-rock_width_max)/2.0) {
            self.rock_left.constant +=top_bottom_space+0.3;
        }
    } else if (button.tag ==703) {
        if (self.rock_left.constant >=left_space_maxX) {
            return;
        }
        self.rock_left.constant +=left_right_space;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
